/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Ventana.java
 *
 * Created on 31/05/2014, 12:27:47
 */
package TP5;

import TP5.TableModel_Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author augusto
 */
public class Ventana extends javax.swing.JFrame {

    private TableModel_Vector[] modelos;
    private VectorEstado vector;
    private Simulador simu;
    private int nroSimulacion = 0;

    /** Creates new form Ventana */
    public Ventana() {
        initComponents();
        btn_AnteriorSimulacion.setEnabled(false);
        btn_SiguienteSimulacion.setEnabled(false);    
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        txt_MinutosASimular = new javax.swing.JTextField();
        txt_MinutosDesde = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txt_CantSimulaciones = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txt_MinutosHasta = new javax.swing.JTextField();
        txt_SimulacionAMostrar = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        btn_Simular = new javax.swing.JButton();
        btn_SiguienteSimulacion = new javax.swing.JButton();
        btn_AnteriorSimulacion = new javax.swing.JButton();
        lbl_simulacion = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tabla.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tabla.getTableHeader().setResizingAllowed(false);
        tabla.getTableHeader().setReorderingAllowed(false);
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);

        jLabel4.setText("Cantidad de minutos a simular:");

        jLabel3.setText("Minuto desde: ");

        jLabel2.setText("Cantidad de Simulaciones:");

        jLabel1.setText("Minuto hasta:");

        jLabel5.setText("Simulación a mostrar:");

        btn_Simular.setText("Simular");
        btn_Simular.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SimularActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5))
                        .addGap(75, 75, 75)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txt_MinutosDesde, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(txt_SimulacionAMostrar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                                    .addComponent(txt_MinutosASimular, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(txt_MinutosHasta, javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(txt_CantSimulaciones, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)))
                                .addGap(24, 24, 24))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btn_Simular)
                        .addContainerGap(324, Short.MAX_VALUE))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txt_MinutosASimular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_MinutosDesde, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_MinutosHasta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txt_CantSimulaciones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt_SimulacionAMostrar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(btn_Simular))
        );

        btn_SiguienteSimulacion.setText("Siguiente-->>");
        btn_SiguienteSimulacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_SiguienteSimulacionActionPerformed(evt);
            }
        });

        btn_AnteriorSimulacion.setText("<<--Anterior");
        btn_AnteriorSimulacion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_AnteriorSimulacionActionPerformed(evt);
            }
        });

        lbl_simulacion.setText("Nro Simulacion");

        jMenu1.setText("Configuracion");

        jMenuItem1.setText("Paramentros de configuración");
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 640, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbl_simulacion)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(199, 199, 199)
                        .addComponent(btn_AnteriorSimulacion, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btn_SiguienteSimulacion, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(50, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbl_simulacion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_SiguienteSimulacion)
                    .addComponent(btn_AnteriorSimulacion))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_SimularActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SimularActionPerformed

        if (validarForm()) {
           
            llenarTabla();
            
        } else {
            JOptionPane.showMessageDialog(this, "Formulario llenado incorrecto ");
        }

    }//GEN-LAST:event_btn_SimularActionPerformed

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked

        tabla.setToolTipText(modelos[0].getColumnName(tabla.getSelectedColumn()));

    }//GEN-LAST:event_tablaMouseClicked

    private void btn_SiguienteSimulacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_SiguienteSimulacionActionPerformed
        if (nroSimulacion < modelos.length) {
            nroSimulacion++;
        }
        cargarTabla();
          
        if (nroSimulacion == modelos.length) {
            btn_SiguienteSimulacion.setEnabled(false);
        }

        btn_AnteriorSimulacion.setEnabled(true);
    }//GEN-LAST:event_btn_SiguienteSimulacionActionPerformed

    private void btn_AnteriorSimulacionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_AnteriorSimulacionActionPerformed
        if (nroSimulacion > 0) {
            nroSimulacion--;
        }
        cargarTabla();
          
        if (nroSimulacion == 0) {
            btn_AnteriorSimulacion.setEnabled(false);
        }

        btn_SiguienteSimulacion.setEnabled(true);
    }//GEN-LAST:event_btn_AnteriorSimulacionActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Ventana().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_AnteriorSimulacion;
    private javax.swing.JButton btn_SiguienteSimulacion;
    private javax.swing.JButton btn_Simular;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbl_simulacion;
    private javax.swing.JTable tabla;
    private javax.swing.JTextField txt_CantSimulaciones;
    private javax.swing.JTextField txt_MinutosASimular;
    private javax.swing.JTextField txt_MinutosDesde;
    private javax.swing.JTextField txt_MinutosHasta;
    private javax.swing.JTextField txt_SimulacionAMostrar;
    // End of variables declaration//GEN-END:variables

    private boolean validarForm() {

        boolean validado = true;
        int cantSimulaciones = 0, minutosASimular = 0, minutosDesde = 0, minutosHasta = 0, simulacionAMostrar;
        try {
            cantSimulaciones = Integer.parseInt(txt_CantSimulaciones.getText());
            minutosASimular = Integer.parseInt(txt_MinutosASimular.getText());
            minutosDesde = Integer.parseInt(txt_MinutosDesde.getText());
            minutosHasta = Integer.parseInt(txt_MinutosHasta.getText());
            simulacionAMostrar = Integer.parseInt(txt_SimulacionAMostrar.getText());

        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Debe ingresar todos numeros enteros");
        }

        if (cantSimulaciones <= 0) {
            validado = false;
        }
        if (minutosASimular <= 0) {
            validado = false;
        }
        if (minutosHasta <= minutosDesde) {
            validado = false;
        }
        if (minutosDesde <= 0) {
            validado = false;
        }

        return validado;
    }

    private void llenarTabla() {
        int cantidadSimulaciones = Integer.parseInt(txt_CantSimulaciones.getText());
        double minutosDesde = Double.parseDouble(txt_MinutosDesde.getText());
        double minutosHasta = Double.parseDouble(txt_MinutosHasta.getText());
        double minutosASimular = Double.parseDouble(txt_MinutosASimular.getText());
        int simulacionAMostrar = Integer.parseInt(txt_SimulacionAMostrar.getText());
        simu = new Simulador(minutosASimular);
        modelos = new TableModel_Vector[cantidadSimulaciones];
        for (int i = 0; i < modelos.length; i++) {
            simu.obtenerSimulacion(minutosDesde, minutosHasta);
            modelos[i] = new TableModel_Vector();
            modelos[i].cargarData(simu.obtenerSimulacion(minutosDesde, minutosHasta));
        }
        nroSimulacion = simulacionAMostrar;
       cargarTabla();
          

    }

    private void cargarTabla() {
        tabla.setModel(modelos[nroSimulacion]);
        lbl_simulacion.setText("Simulación Nro "+nroSimulacion );
    }
}
